global infile "DataMain.dta"
global outfile1 "$results\Table A2A"
global outfile2 "$results\Table A2B"
global outfile3 "$results\Table A2C"


* Create quartiles

use "$infile", clear
drop if dupindicator == 1

keep if year >= 2014
drop if m2 == .
gen m2water = water/m2

drop treat
gen treat = 0
replace treat = 1 if date >= mdy(1,1,2015)

egen M2waterb = mean(m2water) if treat == 0, by(home)
egen m2waterb = mean(M2waterb), by(home)
egen M2watera = mean(m2water) if treat == 1, by(home)
egen m2watera = mean(M2watera), by(home)
gen m2waterm = (m2waterb + m2watera)/2

duplicates drop home, force

_pctile m2waterb, p(25)
local p25 = r(r1)
_pctile m2waterb, p(50)
local p50 = r(r1)
_pctile m2waterb, p(75)
local p75 = r(r1)
gen qb = .
replace qb = 1 if m2waterb <= `p25'
replace qb = 2 if m2waterb > `p25' & m2waterb <= `p50' 
replace qb = 3 if m2waterb > `p50' & m2waterb <= `p75' 
replace qb = 4 if m2waterb > `p75' 

_pctile m2watera, p(25)
local p25 = r(r1)
_pctile m2watera, p(50)
local p50 = r(r1)
_pctile m2watera, p(75)
local p75 = r(r1)
gen qa = .
replace qa = 1 if m2watera <= `p25'
replace qa = 2 if m2watera > `p25' & m2watera <= `p50' 
replace qa = 3 if m2watera > `p50' & m2watera <= `p75' 
replace qa = 4 if m2watera > `p75' 

_pctile m2waterm, p(25)
local p25 = r(r1)
_pctile m2waterm, p(50)
local p50 = r(r1)
_pctile m2waterm, p(75)
local p75 = r(r1)
gen qm = .
replace qm = 1 if m2waterm <= `p25'
replace qm = 2 if m2waterm > `p25' & m2waterm <= `p50' 
replace qm = 3 if m2waterm > `p50' & m2waterm <= `p75' 
replace qm = 4 if m2waterm > `p75' 

keep home qb qa qm

save "Temp", replace


* Merging

use "$infile", clear
keep if year >= 2014
drop if dupindicator == 1
merge m:1 home using "Temp", nogen
save "Temp", replace


* Table A1A. Before-grouping

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015
xtreg logwater treat i.moy if qb == 1, fe i(home) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word replace
xtreg logwater treat i.moy if qb == 2, fe i(home) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qb == 3, fe i(home) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qb == 4, fe i(home) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015 & cohort == 1
reghdfe logwater treat if qb == 1 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qb == 2 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qb == 3 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qb == 4 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile1", keep(treat) nocon 2aster dec(3) word ap


* Table A1B. After-grouping

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015
xtreg logwater treat i.moy if qa == 1, fe i(home) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word replace
xtreg logwater treat i.moy if qa == 2, fe i(home) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qa == 3, fe i(home) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qa == 4, fe i(home) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015 & cohort == 1
reghdfe logwater i.week treat if qa == 1 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qa == 2 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qa == 3 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qa == 4 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile2", keep(treat) nocon 2aster dec(3) word ap


* Table A1B. Our grouping

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015
xtreg logwater treat i.moy if qm == 1, fe i(home) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word replace
xtreg logwater treat i.moy if qm == 2, fe i(home) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qm == 3, fe i(home) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
xtreg logwater treat i.moy if qm == 4, fe i(home) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap

use "Temp", clear
drop treat
gen treat = 0
replace treat = 1 if year >= 2015 & cohort == 1
reghdfe logwater i.week treat if qm == 1 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qm == 2 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qm == 3 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
reghdfe logwater i.week treat if qm == 4 | cohort != 1, absorb(home week) cluster(home)
outreg2 using "$outfile3", keep(treat) nocon 2aster dec(3) word ap
